package com.ruizhong.centercontroll.controller;

import com.ruizhong.centercontroll.aspect.myAnnotation.checkLogin;
import com.ruizhong.centercontroll.entity.TPermission;
import com.ruizhong.centercontroll.entity.TUser;
import com.ruizhong.centercontroll.entity.TUserPermission;
import com.ruizhong.centercontroll.service.TUserService;
import com.ruizhong.centercontroll.service.impl.TUserPermissionServiceImpl;
import com.ruizhong.centercontroll.service.impl.TUserServiceImpl;
import com.ruizhong.centercontroll.util.MD5Hash;
import com.ruizhong.centercontroll.util.Result;
import com.ruizhong.centercontroll.util.ResultCode;
import com.ruizhong.centercontroll.vo.PageSearchVO;
import com.ruizhong.centercontroll.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user/api")
@CrossOrigin
public class TUserController {

    @Autowired
    private TUserService tUserService;

    @PostMapping("/getUserPage")
    @checkLogin("getUserPage")
    public Result getUserPage(@RequestBody PageSearchVO pageSearchVO) {
        PageVO<TUser> userPage = tUserService.getUserPage(pageSearchVO);
        return new Result(ResultCode.success, null, userPage);
    }

    @PostMapping("/submitAddUser")
    @checkLogin("submitAddUser")
    public Result submitAddUser(@RequestBody TUser tUser){
        tUserService.submitAddUser(tUser);
        return new Result(ResultCode.success,"保存用户成功",null);
    }

    @GetMapping("/getPermissionDetailByUserId/{userId}")
    @checkLogin("getPermissionDetailByUserId")
    public Result getPermissionDetailByUserId(@PathVariable(value = "userId") String userId){
        ArrayList<String> userIds = new ArrayList<>();
        userIds.add(userId);
        List<TPermission> tPermissionList = tUserService.getPermissionDetailByUserIds(userIds);
        return new Result(ResultCode.success,null,tPermissionList);
    }

    @PostMapping("/submitNewPermission")
    public Result submitNewPermission(@RequestParam String userId){
        return new Result(ResultCode.success,"保存成功",null);
    }

    @PostMapping("/login")
    public Result login(@RequestBody Map<String,Object> conditionMap){
        String tokenId = tUserService.login(conditionMap);
        return new Result(ResultCode.success,"登录成功",tokenId);
    }

    @PostMapping("/addNewUser")
    @checkLogin("submitAddUser")
    public Result addNewUser(@RequestBody Map<String,String> conditionMap){
        tUserService.addNewUser(conditionMap);
        return new Result(ResultCode.success,"添加成功",null);
    }

    @DeleteMapping("/deleteUser/{username}")
    @checkLogin("deleteUser")
    public Result deleteUser(@PathVariable(value = "username") String username){
        tUserService.deleteUser(username);
        return new Result(ResultCode.success,"删除成功",null);
    }

    @GetMapping("/getDetailByUserId/{id}")
    public Result getDetailByUserId(@PathVariable(value = "id") String id){
        TUser userDetail = tUserService.getDatailByUserId(id);
        return new Result(ResultCode.success,"删除成功",userDetail);
    }

    @PostMapping("/submitUpdate")
    public Result submitUpdate(@RequestBody Map<String,Object> conditionMap){
        tUserService.submitUpdate(conditionMap);
        return new Result(ResultCode.success,"提交成功",null);
    }

    @GetMapping("/getPermissionsByUserId/{userId}")
    @checkLogin("getPermissionDetailByUserId")
    public Result getPermissionsByUserId(@PathVariable(value="userId") String userId){
      TUserPermission tUserPermission = tUserService.getPermissionsByUserId(userId);
    return new Result(ResultCode.success,null,tUserPermission);
    }

    @PostMapping("/updatePermissionsByUserId")
    public Result updatePermissionsByUserId(@RequestBody TUserPermission tUserPermission){
       tUserService.updatePermissionsByUserId(tUserPermission);
        return new Result(ResultCode.success,"修改成功",null);
    }

    @PostMapping("/checkPassword")
    public Result checkPassword(@RequestBody Map<String,String> conditionMap, HttpServletRequest request){
        String authorization = request.getHeader("Authorization");
        System.out.println(authorization);
        TUser user = TUserServiceImpl.tokenMapUser.get(authorization);
        System.out.println(user.getPassword());
        String inputPassword = MD5Hash.md5Hash(conditionMap.get("password"), user.getUsername(), 3);
        if (inputPassword.equals(user.getPassword())){
            return new Result(ResultCode.success,"验证成功",null);
        }
        return new Result(ResultCode.error,"验证失败",null);
    }
}
